
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

/*
 * The front end graphics and clickable interface for point and point-based classes.
 */
public class PointDisplay implements Clickable {
	private Point point;
	ArrayList<Tile> adjTiles;
	private int x;
	private int y;
	SOCApplet applet;
	
	public PointDisplay(Point point, SOCApplet applet){
		this.point = point;
		adjTiles = point.getAdjTiles();
		this.x = findX();
		this.y = findY();
		this.applet = applet;
	}
	
	/*
	 * Finds and returns an x value between 1 and 11 for the point's x-coordinate.
	 */
	public int findX(){
		int x1 = adjTiles.get(0).getX();
		int x2 = adjTiles.get(1).getX();
		int x3 = adjTiles.get(2).getX();
		if ((x1 < x2 && x1 > x3) || x1 > x2 && x1 < x3){
			return x1;
		} else if ((x2 < x1 && x2 > x3) || (x2 > x1 && x2 < x3)){
			return x2;
		} else if ((x3 < x1 && x3 > x2) || (x3 > x1 && x3 < x2)){
			return x3;
		}
		return 0;
	}
	
	/*
	 * Returns int Y (1 - 12) for the y-coordinate of the point.
	 */
	public int findY(){
		//In any correctly set up point, two of the adjacent tiles must share a Y coordinate. Y is determined accordingly.
		if (adjTiles.get(0).getY() == adjTiles.get(1).getY()){
			if (adjTiles.get(2).getY() < adjTiles.get(1).getY()){
				return 2 * adjTiles.get(1).getY() - 1;
			} else {
				return (2 * adjTiles.get(1).getY());
			}
		} else if (adjTiles.get(2).getY() == adjTiles.get(1).getY()){
			if (adjTiles.get(0).getY() < adjTiles.get(1).getY()){
				return 2 * adjTiles.get(1).getY() - 1;
			} else {
				return (2 * adjTiles.get(1).getY());
			}
		} else if (adjTiles.get(0).getY() == adjTiles.get(2).getY()){
			if (adjTiles.get(1).getY() < adjTiles.get(2).getY()){
				return 2 * adjTiles.get(2).getY() - 1;
			} else {
				return (2 * adjTiles.get(2).getY());
			}
		} else {
			return 0;
		}
	}
	
	public void drawSelf(int size, Graphics g){
//		g.setColor(Color.BLACK);
//		g.drawOval(getCircleX(size), getCircleY(size), size, size);
//		g.drawString( " " + y, getCircleX(size) + size / 2, getCircleY(size) + size/2);
//		g.drawOval(size * (1 +findX()) - size / 2,  size * (findY()) - size/2, size, size);
	}

	int getCircleY(int size) {
		/* -1	75
		 * 1	100
		 * 2	150
		 * 3	175
		 * 4	225
		 * 5	250
		 * (SIZE / 2) * (y + (y + 1) % 2)
		 */
		if (y == -1)
			return size;
		else if (y == 12)
			return size * 9;
		return  y * (size) + (size / 2) * ((y + 1) % 2) - (y / 2 - 1) * size / 2;
//		this expression is an ugly motherfucker.
	}

	int getCircleX(int size) {
		// TODO Auto-generated method stub
		return (x + 1) * size - size / 2 + x;
	}

	public void setPoint(Point point) {
		this.point = point;
	}

	public Point getPoint() {
		return point;
	}
	public ArrayList<Tile> getAdjTiles() {
		return adjTiles;
	}
	public void setAdjTiles(ArrayList<Tile> adjTiles) {
		this.adjTiles = adjTiles;
	}
	public int getX() {
		return x;
	}
	public void setX(int x) {
		this.x = x;
	}
	public int getY() {
		return y;
	}
	public void setY(int y) {
		this.y = y;
	}

	@Override
	public boolean overlaps(int px, int py) {
		int size = SOCApplet.SIZE;
		if(px > getCircleX(size) && px < getCircleX(size) + size &&
			py > getCircleY(size) && py < getCircleY(size)+ size){
			System.out.println("Clicked point at" + point.getAdjTiles());
			if(applet.getTurn().getTargetObject() == 'R')
				this.point.startRoad();
			else if (applet.getTurn().getTargetObject() == 'P')
				this.point.endRoad();
			else if (applet.getTurn().getTargetObject() == 'S')
				this.point.buildSettlement(applet.getTurn().getPlayer());
			else if (applet.getTurn().getTargetObject() == 'C')
				this.point.upgradeSettlement(applet.getTurn().getPlayer());
			return true;
		}
		return false;
	}
	
	
}
